import { Config } from "../../src/config/Config";
import { DbConfig } from "../../src/config/DbConfig";
import { DaoBase } from "../../src/engine/dao/DaoBase";
import DateUtil from "../../src/engine/utils/DateUtil";
import { Logger } from "../../src/engine/utils/Logger";
import { MysqlClient } from "../../src/engine/utils/MySqlClient";
import { MysqlManager } from "../../src/engine/utils/MysqlManager";
import { GameResult } from "../model/GameResult";
import { VideoInfo } from "../model/VideoInfo";

export class RoomVideoDao extends DaoBase {

    public static DB_NAME: string = DbConfig.Db_Name.db_admin;
    public static Tb_Name: string = "t_room_video";

    public static async insertVideo(model: VideoInfo) {
        if(!model.isSave){
            model.isSave = true
            let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
            let sql: string = "insert into t_room_video (gameResultId, ju, time, videoResult, playerList, begin_gameInfo, actionList) VALUES(?,?,?,?,?,?,?) ;"
            let sqlParams: Array<any> = [model.gameResultId, model.ju, DateUtil.formatDateStr(model.time),
            JSON.stringify(model.videoResult), JSON.stringify(model.playerList), JSON.stringify(model.begin_gameInfo),
            JSON.stringify(model.actionList)];
            let rows = await client.queryPromise(sql, sqlParams);
            // Logger.log("insertVideo====", rows)
            if (rows && rows["affectedRows"] > 0) {
                return true
            } else {
                return false
            }
        }
    }

    public static async checkClear(delete_time: number) {
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let sql: string = "delete from t_room_video where time < ?"
        let sqlParams = [DateUtil.formatDateStr(delete_time)];
        await  client.queryPromise(sql, sqlParams);
    }

    public static async getByGameResultId(gameResultId: number) {
        let sql: string = "select * from  t_room_video where `gameResultId`=?"
        let sqlParams = [gameResultId];
        let client: MysqlClient = await MysqlManager.instance.getDb(this.DB_NAME);
        let rows = await client.queryPromise(sql, sqlParams);
        Logger.log("getByGameResultId", sql, sqlParams, rows)
        if (rows && rows.length > 0) {
            return rows
        }
        return []
    }

}